package com.wxl.common.wiget

import android.content.Context
import android.view.KeyEvent
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.wxl.common.R
import com.wxl.common.base.AbsActivity
import io.reactivex.functions.Action
import kotlinx.android.synthetic.main.pop_no_auth_tip.view.*

class NotAuthTipPop(context: Context) : CenterPopupView(context) {

    lateinit var noAuthNum : String
    lateinit var fialedAuthNum:String
    lateinit var action0:Action
    lateinit var action1:Action

    override fun getImplLayoutId(): Int {
        return R.layout.pop_no_auth_tip
    }

    override fun onCreate() {
        super.onCreate()
        noAuthTipText.text = noAuthNum
        noAuthFailTipText.text = fialedAuthNum
        noAuthTipCancel.setOnClickListener {
            dismiss()
            action0.run()
        }
        noAuthTipSure.setOnClickListener {
            dismiss()
            action1.run()
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if(keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_F4){
            noAuthTipSure.performClick()
        }
        if(keyCode == KeyEvent.KEYCODE_BACK|| keyCode == KeyEvent.KEYCODE_F1){
            noAuthTipCancel.performClick()
        }
        return true
    }

    companion object {
        fun show(context: Context,noAuthNum : String,fialedAuthNum:String,cancelAction:Action,sureAction:Action){
            val pop = NotAuthTipPop(context)
            pop.action0 = cancelAction
            pop.action1 = sureAction
            pop.fialedAuthNum = fialedAuthNum
            pop.noAuthNum = noAuthNum
            (context as AbsActivity).addPop(
            XPopup.Builder(context)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .isRequestFocus(false)
                .asCustom(pop)
                .show())
        }
    }
}